import { Data } from "../global/data";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

  @property([cc.Prefab])
  goldGroup:cc.Prefab[]=[]

  onLoad() {
    if(!this.goldGroup){
      cc.log('gold group is null');
      this.enabled=false;
    }
  }

  
  start(){
    //随机生成金币
    if(Math.random()<0.33){
      let i=(Math.random()*this.goldGroup.length) | 0;
      let gold=cc.instantiate(this.goldGroup[i]);
      if(gold.children.length>0){
        for(let g of gold.children){
          g.x+=this.node.x;
          g.y+=this.node.y+100;
          let rigbody=g.getComponent(cc.RigidBody);
          rigbody.linearVelocity=cc.v2(-Data.speed,0);
        }
      }
      else{
        gold.x=this.node.x;
        gold.y=this.node.y+100;
        let rigbody=gold.getComponent(cc.RigidBody);
        rigbody.linearVelocity=cc.v2(-Data.speed,0);
      }
     
      this.node.parent.addChild(gold);
      
    }
  }

  // onBeginContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider) {

  //   if (other.node.group == "back") {
  //     // 重新生成
  //     //self.node.parent.emit('contact');
  //     //self.node.removeFromParent();
      
  //   }
  // }
}